//
//  MainMenuLayer.m
//  SpeedTap
//
//  Created by Chi Ho Chiu on 18/8/11.
//  Copyright 2011 Individual. All rights reserved.
//

#import "MainMenuLayer.h"

@implementation MainMenuLayer
@synthesize delegate;

-(id) init
{
    if( (self = [super init]) )
    {
		self.isTouchEnabled = YES;
		
		//Add background image
        CCSprite *bgSprite = [CCSprite spriteWithFile:@"opening.png"];
        [bgSprite setPosition:CGPointMake(240,160)];
        [self addChild:bgSprite z:0];

		//Add Play Sprite
		CCSprite *playSprite = [CCSprite node];
		[playSprite setOpacity:128];
		[playSprite setColor:ccc3(0, 250, 0)];
		[playSprite setTextureRect:CGRectMake(0, 0, 130, 90)];
		[playSprite setPosition:CGPointMake(405, 195)];
		[self addChild:playSprite z:1 tag:kMAIN_MENU_TAG_PLAY];

		//Add Gallery Sprite
		CCSprite *gallerySprite = [CCSprite node];
		[gallerySprite setOpacity:128];
		[gallerySprite setColor:ccc3(0, 250, 0)];
		[gallerySprite setTextureRect:CGRectMake(0, 0, 100, 70)];
		[gallerySprite setPosition:CGPointMake(280, 155)];
		[self addChild:gallerySprite z:1 tag:kMAIN_MENU_TAG_GALLERY];
			
		//Add Credit Sprite
		CCSprite *creditSprite = [CCSprite node];
		[creditSprite setOpacity:128];
		[creditSprite setColor:ccc3(0, 250, 0)];
		[creditSprite setTextureRect:CGRectMake(0, 0, 135, 80)];
		[creditSprite setPosition:CGPointMake(385, 60)];
		[self addChild:creditSprite z:1 tag:kMAIN_MENU_TAG_CREDIT];
    }
    
    return self;
}

-(void) dealloc
{
    [super dealloc];
}

#pragma mark - Touch Event
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
	UITouch* touch = [touches anyObject];
	CGPoint touchLocation =[[CCDirector sharedDirector] convertToGL:[touch locationInView:[touch view]]];
	
	if( CGRectContainsPoint([[self getChildByTag:kMAIN_MENU_TAG_PLAY] boundingBox], touchLocation) )
	{
		[delegate onPlaySelected];
	}
	else if (CGRectContainsPoint([[self getChildByTag:kMAIN_MENU_TAG_GALLERY] boundingBox], touchLocation) )
	{
		[delegate onGallerySelected];
	}
	else if (CGRectContainsPoint([[self getChildByTag:kMAIN_MENU_TAG_CREDIT] boundingBox], touchLocation) )
	{
		[delegate onCreditSelected];
	}
}

@end
